<?php

namespace app\admin\controller;


use app\common\base\AuthenticatedController;
use app\common\base\Form;
use think\Db;
use think\Error;


/**
 * 设备分类控制器
 * Class DeviceClassifyController
 * @package app\admin\controller
 */
class DeviceClassifyController extends AuthenticatedController {

    public function Add() {

        Form::Validate([
            'name'  =>  'require',
        ]);

        $insert_data = [
            'name'  =>  input('name'),
        ];

        $result = Db::table('device_classify')->insert($insert_data);
        if (!$result) {
            return Error('添加数据发生错误');
        }

        return Success('添加成功', Db::table('device_classify')->getLastInsID());

    }

    public function Modify() {

        Form::Validate([
            'id'    =>  'require|integer',
            'name'  =>  'require',
        ]);

        $result = Db::table('device_classify')->where('id', input('id'))->find();
        if (empty($result)) {
            return Error('不存在的设备分类');
        }
        unset($result['id']);
        $result['name'] = input('name');
        if (!Db::table('device_classify')->where('id', input('id'))->update($result)) {
            return Error('保存失败');
        }

        return Success('保存成功', $result);

    }

    public function Delete() {

        Form::Validate([
            'id'    =>  'require|integer'
        ]);

        $result = Db::table('device_classify')->where('id', input('id'))->delete();
        if (!$result) {
            return Error('删除失败');
        }

        return Success('删除成功');
    }

    public function GetList() {
        $result = Db::table('device_classify')->select();

        return Success('获取成功', $result);
    }


    public function GetOne() {

        Form::Validate([
            'id'    =>  'require|integer'
        ]);

        $result = Db::table('device_classify')->where('id', input('id'))->find();
        if (!$result) {
            return Error('获取失败');
        }

        return Success('获取成功', $result);

    }

}